package cn.stronglink.assetmanage.business.check.view.detail;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

import java.io.FileNotFoundException;
import java.util.List;

import Scaner.rfid.IRfidListener;
import Scaner.rfid.TimerRfidScaner;
import Scaner.scan.IScanerListener;
import Scaner.scan.TimerInfraraedScaner;
import cn.stronglink.assetmanage.R;
import cn.stronglink.assetmanage.business.check.model.CheckDetailModel;
import cn.stronglink.assetmanage.business.check.presenter.CheckDetailPresenter;
import cn.stronglink.assetmanage.business.check.view.checked.CheckedAssetFragment;
import cn.stronglink.assetmanage.entity.Asset;
import cn.stronglink.assetmanage.entity.Brand;
import cn.stronglink.assetmanage.entity.Room;
import cn.stronglink.assetmanage.entity.Type;
import cn.stronglink.assetmanage.index.base.BaseChildFragment;
import cn.stronglink.assetmanage.utils.Constants;
import cn.stronglink.assetmanage.utils.ImageUtils;
import cn.stronglink.assetmanage.utils.ToastUtil;

import static android.app.Activity.RESULT_CANCELED;
import static android.app.Activity.RESULT_OK;

/**
 * 入库验收详情
 */
public class CheckedDetailFragment extends BaseChildFragment implements View.OnClickListener {
    private Asset asset;
    private CheckDetailModel checkDetailModel;
    private CheckDetailPresenter checkDetailPresenter;
    private TextView txt_title_right, txt_title_left, txt_main_title;
    private TextView tv_check_asset_name, tv_check_location, tv_check_asset_no, tv_brand_name, tv_check_asset_class, tv_check_model, tv_check_sn_no, tv_check_rfid_no;
    private ImageView iv_check_picture;

    @Nullable
    @Override

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_check_sync_detail, container, false);
        initViewAndData(view);
        return view;
    }

    protected Activity mActivity;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.mActivity = (Activity) context;
    }

    private void initViewAndData(View view) {
        try {
            txt_main_title = this.getActivity().findViewById(R.id.txt_main_title);
            txt_main_title.setText("已入库");
            txt_title_right = this.getActivity().findViewById(R.id.txt_right_title);
            txt_title_right.setVisibility(View.INVISIBLE);
            txt_title_left = this.getActivity().findViewById(R.id.txt_left_title);
            txt_title_left.setVisibility(View.VISIBLE);
            asset = new Asset();
            tv_check_asset_name = view.findViewById(R.id.tv_check_asset_name);
            tv_check_location = view.findViewById(R.id.tv_check_location);
            tv_check_asset_no = view.findViewById(R.id.tv_check_asset_no);
            tv_brand_name = view.findViewById(R.id.tv_brand_name);
            tv_check_asset_class = view.findViewById(R.id.tv_check_asset_class);
            tv_check_model = view.findViewById(R.id.tv_check_model);
            tv_check_sn_no = view.findViewById(R.id.tv_check_sn_no);
            tv_check_rfid_no = view.findViewById(R.id.tv_check_rfid_no);
            iv_check_picture = view.findViewById(R.id.iv_check_picture);
            iv_check_picture = view.findViewById(R.id.iv_check_picture);
            iv_check_picture.setOnClickListener(this);
            checkDetailModel = new CheckDetailModel();
            Bundle bundle = getArguments();
            if (bundle != null) {
                long asset_id = bundle.getLong(Constants.CHECK_IN_DETAIL_NO);
                asset = checkDetailModel.getAssetById(asset_id);
                showAssetDeatil();
            } else {
                ToastUtil.toastShort(getString(R.string.get_no_data));
            }

            txt_title_left = this.getActivity().findViewById(R.id.txt_left_title);
            txt_title_left.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mActivity.onBackPressed();
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
            ToastUtil.toastShort("没有数据显示");
        }
    }

    private void showAssetDeatil() {
        if (asset != null) {
            tv_check_asset_name.setText(asset.getAssetName());
            tv_check_location.setText(asset.getRoomName());
            tv_check_asset_no.setText(asset.getAssetNo());
            tv_brand_name.setText(asset.getBrandName());
            tv_check_asset_class.setText(asset.getTypeName());
            tv_check_model.setText(asset.getModelName());
            tv_check_sn_no.setText(asset.getSerialNo());
            tv_check_rfid_no.setText(asset.getRfidNo());
            String url = asset.getPictureUri();
            Bitmap bitmap = null;
            if (url != null) {
                try {
                    bitmap = checkDetailModel.getBitmapByUrl(url);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            }
            if (bitmap != null) {
                iv_check_picture.setImageBitmap(bitmap);
            }else{
                iv_check_picture.setVisibility(View.VISIBLE);
            }
        }
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_check_asset_no:
                // 资产编号扫描

                break;
            case R.id.tv_check_sn_no:
                //sn号扫描

                break;
            case R.id.tv_check_rfid_no:
                // rfid扫描


                break;
            case R.id.iv_check_picture:
                break;
        }
    }
}
